package com.skivingcloud.develop.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.develop.entity.SysBusinessDesignerComponent;
import com.skivingcloud.develop.entity.SysFormDesigner;
import com.skivingcloud.develop.mapper.SysBusinessDesignerComponentMapper;
import com.skivingcloud.develop.service.SysBusinessDesignerComponentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.skivingcloud.common.utils.PageParameter;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2025-01-13
 */
@Service
public class SysBusinessDesignerComponentServiceImpl extends ServiceImpl<SysBusinessDesignerComponentMapper, SysBusinessDesignerComponent> implements SysBusinessDesignerComponentService {

    /**
     * 分页查询
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysBusinessDesignerComponent> queryByPage(PageParameter<SysBusinessDesignerComponent> parameter){
        Page<SysBusinessDesignerComponent> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (StringUtils.isNoneBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysBusinessDesignerComponent::getId).like(parameter.getParam().getId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getDesignerId())) {
                queryWrapper.and(SysBusinessDesignerComponent::getDesignerId).like(parameter.getParam().getDesignerId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getDesignerCode())) {
                queryWrapper.and(SysBusinessDesignerComponent::getDesignerCode).like(parameter.getParam().getDesignerCode());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getComponentId())) {
                queryWrapper.and(SysBusinessDesignerComponent::getComponentId).like(parameter.getParam().getComponentId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getComponentCode())) {
                queryWrapper.and(SysBusinessDesignerComponent::getComponentCode).like(parameter.getParam().getComponentCode());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 根据表单设计id获取表单下的组件信息
     *
     * @param designerId 表单信息
     * @return 表单下的组件信息
     */
    @Override
    public List<SysBusinessDesignerComponent> queryByDesignerId(String designerId) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(SysBusinessDesignerComponent.class)
                .join(SysFormDesigner.class).on(
                        wrapper -> wrapper.where(SysBusinessDesignerComponent::getComponentId)
                                .eq(SysFormDesigner::getId)
                ).where(SysBusinessDesignerComponent::getDesignerId).eq(designerId);
        queryWrapper.orderBy(SysBusinessDesignerComponent::getOrderNum);
        return this.list(queryWrapper);
    }
}
